package com.capgemini.diabeo;

import java.util.ArrayList;
import java.util.HashMap;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.capgemini.diabeo.dummy.DummyContent;
import com.capgemini.diabeo.dummy.DummyContent.DishesItem;

/**
 * A fragment representing a single AddDish detail screen. This fragment is
 * either contained in a {@link AddDishListActivity} in two-pane mode (on
 * tablets) or a {@link AddDishDetailActivity} on handsets.
 */
public class AddDishDetailFragment extends Fragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	private ListView lv;
	DishesAdapter adapter;
	ArrayList<HashMap<String, String>> dishesList;
	HashMap<String, String> dish;
	public static final String ARG_ITEM_ID = "item_id";

	/**
	 * The dummy content this fragment is presenting.
	 */
	private DummyContent.DishesItem mItem;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public AddDishDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			// Load the dummy content specified by the fragment
			// arguments. In a real-world scenario, use a Loader
			// to load content from a content provider.
			mItem = DummyContent.ITEM_MAP.get(getArguments().getString(
					ARG_ITEM_ID));
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_adddish_detail,
				container, false);

		lv = (ListView) rootView.findViewById(R.id.dishesList);

		dishesList = new ArrayList<HashMap<String,String>>();

		if (mItem != null) {
			Cursor cursorDishName = readDishes(mItem);

			if (cursorDishName.moveToFirst()) {
				do {
					String nom = cursorDishName.getString(cursorDishName.getColumnIndex(AlimentsEntry.COLUMN_NAME_NOM));
					String id = cursorDishName.getString(cursorDishName.getColumnIndex(AlimentsEntry.COLUMN_NAME_ALIMENT_ID));
					String quantite = cursorDishName.getString(cursorDishName.getColumnIndex(AlimentsEntry.COLUMN_NAME_QUANTITE));
					String glucide = cursorDishName.getString(cursorDishName.getColumnIndex(AlimentsEntry.COLUMN_NAME_GLUCIDE));
					String lipide = cursorDishName.getString(cursorDishName.getColumnIndex(AlimentsEntry.COLUMN_NAME_LIPIDE));
					String ig = cursorDishName.getString(cursorDishName.getColumnIndex(AlimentsEntry.COLUMN_NAME_IG));

					dish = new HashMap<String, String>();
					dish.put(AlimentsEntry.COLUMN_NAME_NOM, nom);
					dish.put(AlimentsEntry.COLUMN_NAME_ALIMENT_ID, id);
					dish.put(AlimentsEntry.COLUMN_NAME_QUANTITE, quantite);
					dish.put(AlimentsEntry.COLUMN_NAME_GLUCIDE, glucide);
					dish.put(AlimentsEntry.COLUMN_NAME_LIPIDE, lipide);
					dish.put(AlimentsEntry.COLUMN_NAME_IG, ig);

					dishesList.add(dish);
				} while (cursorDishName.moveToNext());
			}

			adapter = new DishesAdapter(this.getActivity(), dishesList);
			lv.setAdapter(adapter);
		}

		return rootView;
	}

	public static Cursor readDishes(DishesItem mItem)
	{ 
		SQLiteDatabase dbread = DummyContent.dbOpenHelper.getReadableDatabase();
		String[] projection = {
				AlimentsEntry.COLUMN_NAME_NOM,
				AlimentsEntry.COLUMN_NAME_ALIMENT_ID,
				AlimentsEntry.COLUMN_NAME_QUANTITE,
				AlimentsEntry.COLUMN_NAME_GLUCIDE,
				AlimentsEntry.COLUMN_NAME_LIPIDE,
				AlimentsEntry.COLUMN_NAME_IG};
		/*  String sortOrder = AlimentsEntry.COLUMN_NAME_ALIMENT_ID + " DESC";*/

		Cursor c = dbread.query(
				AlimentsEntry.TABLE_NAME,
				projection,
				AlimentsEntry.COLUMN_NAME_CATEGORIE + " like " + "\"" + mItem.content + "\"" ,
				null,
				AlimentsEntry.COLUMN_NAME_NOM,
				null,
				null
				);
		return c;    
	}
}
